# coding: utf-8
# @Author: cyl
# @File: camera_del.py
# @Time: 2022-06-08
import os
import pathlib


class cdUpDir:
    def __init__(self, newPath):
        self.newPath = os.path.expanduser(newPath)

    def __enter__(self):
        self.savedPath = os.getcwd()
        os.chdir(self.newPath)

    def __exit__(self, etype, value, traceback):
        os.chdir(self.savedPath)


class cdDownDir:
    def __init__(self, path):
        self.path = os.path.expanduser(path)
    
    def __enter__(self):
        os.chdir(self.path + "/src")

    def __exit__(self, etype, value, traceback):
        os.chdir(self.path)


# 删除__pycache__文件
def del_pycache_file():
    for p in pathlib.Path('..').rglob('*.py[co]'):
        p.unlink()
    for p in pathlib.Path('..').rglob('__pycache__'):
        p.rmdir()


# 清理远程仓库.idea缓存
def del_idea_cached():
    try:
        current = os.getcwd()
        # 获取/src父级节点路径
        parent = os.path.abspath(os.path.dirname(current) + os.path.sep + ".")
        with cdUpDir(parent):
            result = os.system("git rm -r --cached .idea")
    except Exception as e:
        pass
